//
//  MYGViewController.swift
//  MYDoctor
//
//  Created by Denny on 2024/5/24.
//

import UIKit
import MJRefresh
import JXSegmentedView

class MYGAcademicConferenceListVC: MYGBaseController ,JXSegmentedListContainerViewListDelegate{
    var page = 1
    var dataSource = [MYGMeetList]()
    var style = 1
    var tag = 100
    var is_heat = ""
    var is_time = "1"
    
    func listView() -> UIView {
        return self.view
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "学术会议"
        NotificationCenter.default.addObserver(self, selector: #selector(reloadStyle(_:)), name: Notification.Name("MYGAcademicConferenceListVC_reloadStyle"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(refreshData(_:)), name: Notification.Name("MYGAcademicConferenceListVC_refreshData"), object: nil)
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        reloadCollectionViewLayout()
        self.getList()
    }
    lazy var collectionView: UICollectionView = {
     
        let control = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
        control.register(UINib.init(nibName: "MYGAcademicListCell1", bundle: nil), forCellWithReuseIdentifier: "MYGAcademicListCell1")
        control.register(UINib.init(nibName: "MYGAcademicListCell2", bundle: nil), forCellWithReuseIdentifier: "MYGAcademicListCell2")
        control.showsHorizontalScrollIndicator = false
        control.showsVerticalScrollIndicator = false
        control.backgroundColor = .white
        control.delegate = self
        control.dataSource = self

        return control
    }()

    @objc func refreshData(_ no:Notification){
        if let userinfo = no.userInfo {
            let tag = userinfo["tag"] as? Int
            if tag == self.tag {
                if let time = userinfo["is_time"] as? String,let heat = userinfo["is_heat"] as? String {
                    self.is_time = time
                    self.is_heat = heat
                    getList()
                }
            }
        }
    }
    
    @objc func getList() {
        if ((MYGUserManager.getUser()?.doctor.name.isEmpty) == true)  && self.tag == 101{
            return
        }
        if self.tag == 101 {
            if MYGUserManager.isDoctorInfComplete  {
                if MYGUserManager.getUser()?.doctor.status == 20 {
                }else {
                    return
                }
            }else {
                return
            }
        }
       
        let par = ["is_my":self.tag == 100 ? "" : "1","is_time":is_time,"is_heat":is_heat]
        MYGLoading.show()
        MYGNetwork<MYGHomePageAPI, MYGPageModel<MYGMeetList>>.request(target: .meetList(param: par)) { result in
            MYGLoading.hide()
            self.dataSource = result.list
            self.collectionView.reloadData()
            self.collectionView.mj_header?.endRefreshing()
        } failed: { _, error in
            self.collectionView.mj_header?.endRefreshing()
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    @objc func reloadStyle(_ sender:Notification){
        if let index = sender.userInfo?["style"] as? Int,let tag = sender.userInfo?["tag"] as? Int {
            if tag == self.tag {
                self.style = index
                self.reloadCollectionViewLayout()
            }
        }
    }
    func reloadCollectionViewLayout() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        var leftMargin = 0.0
        var midMargin = 0.0
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        if style == 1 {
            //标准  横排
            leftMargin = 0
            midMargin = 0
            layout.minimumLineSpacing = 0
            layout.minimumInteritemSpacing = 0
            let itemWidth = screenWidth
            layout.itemSize = CGSize(width: itemWidth, height: 103)
        }else {
            //标准 方块
             leftMargin = 20
             midMargin = 12
            layout.minimumLineSpacing = 12
            layout.minimumInteritemSpacing = 6
            let itemWidth =  (screenWidth - 2 * leftMargin - 1 * midMargin) / 2
            layout.itemSize = CGSize(width: itemWidth, height: 184)
        }
        self.collectionView.contentInset = UIEdgeInsets(top: 0, left: leftMargin, bottom: 0, right: leftMargin)
        self.collectionView.setCollectionViewLayout(layout, animated: false)
        self.collectionView.reloadData()
    }
    
}
extension MYGAcademicConferenceListVC:UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if self.style == 1 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGAcademicListCell1", for: indexPath) as! MYGAcademicListCell1
            let model = self.dataSource[indexPath.row]
            cell.model = model
            return cell
        }else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGAcademicListCell2", for: indexPath) as! MYGAcademicListCell2
            let model = self.dataSource[indexPath.row]
            cell.model = model
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = MYGAcademicConferenceDetailVC()
        let model = self.dataSource[indexPath.row]
        vc.meeting_id = "\(model.id)"
        self.navigationController?.pushViewController(vc)
    }
}
